package com.pwc.conversionTool.rest;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.Manager.XMLConfigImpl;
import com.pwc.conversionTool.task.Task;
import com.pwc.conversionTool.task.TaskManage;

@Controller
public class TaskRest {
	private static final Logger logger = LoggerFactory
			.getLogger(TaskRest.class);
	
	
	@Autowired
	public ServletContext context;
	
	/**
	 * start a task
	 * 
	 * @return
	 * @throws DocumentException 
	 * @throws IOException 
	 */
	@RequestMapping(value = "/rest/task/startTask", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
	public @ResponseBody String startTask(String taskName,String csvFileName,String csvFilePath,String relationFileName,String relationFilePath) throws IOException, DocumentException {

		List<String> csvFiles=new ArrayList<String>();
		String relationFile=relationFilePath+File.separator+relationFileName;
		JSONArray jsonarray=JSONArray.fromObject(csvFileName);
		JSONObject jsonObject=jsonarray.getJSONObject(0);
		Iterator iterator=jsonObject.keys();
		String key=null;
		String value=null;

		while (iterator.hasNext()) {
            key = (String) iterator.next();
            value = jsonObject.getString(key);
            String csvFile=csvFilePath+File.separator+value;
            csvFiles.add(csvFile);
            System.out.println(relationFile);
        }
		TaskManage.getInstance().startTask(context,csvFiles,relationFile,taskName);
		String xmlData = "success!";
		return xmlData;
	}
	
	@RequestMapping(value = "/rest/task/manageTask", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody Map<String, String> manageTask() {
		Map<String, String> hashMap = new HashMap<String, String>();
		List<Task> tasks=TaskManage.getTasks();
		if(tasks==null){
			return null;
		}
		else{
			for(Task task:tasks){
				hashMap.put(task.getTaskName(),task.getTaskStatus());
			}
			return hashMap;
		}
		
	}
	
	@RequestMapping(value = "/rest/task/stopOneTask", method = RequestMethod.GET, produces = "application/json")
	public @ResponseBody String stopOneTask(String taskName) {
		List<Task> tasks=TaskManage.getTasks();
		for(Task task:tasks){
			if(taskName.equals(task.getTaskName())){
				task.setStop(true);
			}
		}
		return "stop task success!";
	}
}
